package com.example.study.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.study.domain.*;
import com.example.study.service.*;
import com.example.study.service.vo.LoginBody;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequiredArgsConstructor
public class LoginController {

    private final IStudentService studentService;

    private final IUserService userService;

    private final ITeacherService teacherService;

    private final ISelectCourseService selectCourseService;

    private final ICourseService courseService;

    private final IClasszService classzService;

    @GetMapping({"/","/login"})
    public String loginPage(HttpSession httpSession){
        clearSessionData(httpSession);
        return "login";
    }
    @PostMapping("/login")
    public String login(LoginBody loginBody, HttpSession httpSession){
        clearSessionData(httpSession);
        UserDO oneUserDO = userService.getOne(new LambdaQueryWrapper<UserDO>()
                .eq(UserDO::getType, loginBody.getType())
                .eq(UserDO::getUsername, loginBody.getUsername())
                .eq(UserDO::getPassword, loginBody.getPassword()));
        if (BeanUtil.isEmpty(oneUserDO)) {
            httpSession.setAttribute("msg","用户名或密码错误");
            return "login";
        }
        if ("teacher".equalsIgnoreCase(oneUserDO.getType())) {
            TeacherDO oneTeacherDO = teacherService.getOne(new LambdaQueryWrapper<TeacherDO>()
                    .eq(TeacherDO::getUserId, oneUserDO.getId()));
            httpSession.setAttribute("teacher",oneTeacherDO);
        }
        if ("student".equalsIgnoreCase(oneUserDO.getType())) {
            StudentDO oneStudentDO = studentService.getOne(new LambdaQueryWrapper<StudentDO>()
                    .eq(StudentDO::getUserId, oneUserDO.getId()));
            httpSession.setAttribute("student",oneStudentDO);
            // 设置班级信息
            ClasszDO byIdClasszDO = classzService.getById(oneStudentDO.getClassId());
            // 设置课程信息
            List<Long> selectedId = selectCourseService.list(new LambdaQueryWrapper<SelectCourseDO>()
                            .eq(SelectCourseDO::getStudentId, oneStudentDO.getId()))
                    .stream().map(item -> item.getCourseId()).collect(Collectors.toList());
            if (selectedId.size() > 0) {
                List<CourseDO> selectedCourse = courseService.listByIds(selectedId);
                selectedCourse = selectedCourse.stream().map(item -> {
                    item.setClasszName(byIdClasszDO.getName());
                    TeacherDO byIdTeacherDO = teacherService.getById(item.getTeacherId());
                    item.setTeacherName(byIdTeacherDO.getName());
                    return item;
                }).collect(Collectors.toList());

                httpSession.setAttribute("selectedCourse",selectedCourse);
            }
        }
        if ("manager".equalsIgnoreCase(oneUserDO.getType())) {
            httpSession.setAttribute("manager",oneUserDO);
        }
        return "redirect:/index";
    }

    private void clearSessionData(HttpSession httpSession) {
        httpSession.removeAttribute("teacher");
        httpSession.removeAttribute("student");
        httpSession.removeAttribute("manager");
        httpSession.removeAttribute("msg");
    }
}
